<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 7.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"jjbond.cn","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"right","width":300,"display":"post","padding":18,"offset":12,"onmobile":false,"sidebar_rss":{"text":"RSS","icon":"fa fa-rss","color":"burlywood","link":"atom.xml"}},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="2023&#x2F;11&#x2F;26 this指针&#x2F;闭包&#x2F;作用域面试时必知必会    JS 布局 端(小程序、web) 技术栈(Vue、React)  争取学习    辅助技术栈 热门模块和工程化  为我所用实战 | 算法 | 模式">
<meta property="og:type" content="article">
<meta property="og:title" content="this指针&#x2F;闭包&#x2F;作用域">
<meta property="og:url" content="https://jjbond.cn/posts/4503.html">
<meta property="og:site_name" content="吉吉邦德">
<meta property="og:description" content="2023&#x2F;11&#x2F;26 this指针&#x2F;闭包&#x2F;作用域面试时必知必会    JS 布局 端(小程序、web) 技术栈(Vue、React)  争取学习    辅助技术栈 热门模块和工程化  为我所用实战 | 算法 | 模式">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ooo.0x0.ooo/2023/11/27/OrDIMi.png">
<meta property="og:image" content="https://ooo.0x0.ooo/2023/11/28/OrGWMC.png">
<meta property="article:published_time" content="2023-11-25T03:26:38.000Z">
<meta property="article:modified_time" content="2024-04-15T07:42:55.075Z">
<meta property="article:author" content="右眼">
<meta property="article:tag" content="Web">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ooo.0x0.ooo/2023/11/27/OrDIMi.png">

<link rel="canonical" href="https://jjbond.cn/posts/4503.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>this指针/闭包/作用域 | 吉吉邦德</title>

  <!-- 球形气泡标签云 -->
  <script type="text/javascript" src="/js/bubble.js"></script>

  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="吉吉邦德" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
      <a target="_blank" rel="noopener" href="https://github.com/Neopo1itan/" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#1f3e5c; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a ><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">吉吉邦德</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-regular fa-desktop fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-regular fa-tags fa-fw"></i>标签<span class="badge">7</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-regular fa-shapes fa-fw"></i>分类<span class="badge">6</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-regular fa-inbox fa-fw"></i>归档<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-rss">

    <a href="/atom.xml" rel="section"><i class="fa fa-rss fa-fw"></i>RSS</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://jjbond.cn/posts/4503.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="右眼">
      <meta itemprop="description" content="任何时间 任何地点<br>超级侦探 认真办案">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="吉吉邦德">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          this指针/闭包/作用域
        </h1>

        <div class="post-meta">
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-11-25 11:26:38" itemprop="dateCreated datePublished" datetime="2023-11-25T11:26:38+08:00">2023-11-25</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2024-04-15 15:42:55" itemprop="dateModified" datetime="2024-04-15T15:42:55+08:00">2024-04-15</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JS/" itemprop="url" rel="index"><span itemprop="name">JS</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.6k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="2023-11-26-this指针-闭包-作用域"><a href="#2023-11-26-this指针-闭包-作用域" class="headerlink" title="2023&#x2F;11&#x2F;26 this指针&#x2F;闭包&#x2F;作用域"></a>2023&#x2F;11&#x2F;26 this指针&#x2F;闭包&#x2F;作用域</h1><p><strong>面试时必知必会</strong>  </p>
<ol>
<li>JS</li>
<li>布局</li>
<li>端(小程序、web)</li>
<li>技术栈(Vue、React)</li>
</ol>
<p><strong>争取学习</strong>  </p>
<ol>
<li>辅助技术栈</li>
<li>热门模块和工程化</li>
</ol>
<p><strong>为我所用</strong><br>实战 | 算法 | 模式</p>
<span id="more"></span>
<ul>
<li><a href="#20231126-this%E6%8C%87%E9%92%88%E9%97%AD%E5%8C%85%E4%BD%9C%E7%94%A8%E5%9F%9F">2023&#x2F;11&#x2F;26 this指针&#x2F;闭包&#x2F;作用域</a><ul>
<li><a href="#%E4%BD%9C%E7%94%A8%E5%9F%9F--%E4%B8%8A%E4%B8%8B%E6%96%87">作用域 &amp; 上下文</a><ul>
<li><a href="#%E4%BD%9C%E7%94%A8%E5%9F%9F%E9%93%BE">作用域链</a></li>
<li><a href="#this-%E4%B8%8A%E4%B8%8B%E6%96%87-context">this 上下文 context</a><ul>
<li><a href="#%E5%87%BD%E6%95%B0%E7%9B%B4%E6%8E%A5%E8%B0%83%E7%94%A8---this%E6%8C%87%E5%90%91%E6%98%AFwindow">函数直接调用 - this指向是window</a></li>
<li><a href="#%E9%9A%90%E5%BC%8F%E7%BB%91%E5%AE%9A---this%E6%8C%87%E5%90%91%E8%B0%83%E7%94%A8%E5%A0%86%E6%A0%88%E7%9A%84%E4%B8%8A%E4%B8%80%E7%BA%A7">隐式绑定 - this指向调用堆栈的上一级</a></li>
<li><a href="#%E9%9D%A2%E8%AF%95%E9%A2%98">面试题</a></li>
<li><a href="#%E6%98%BE%E7%A4%BA%E7%BB%91%E5%AE%9Abind--apply--call">显示绑定(bind | apply | call)</a></li>
<li><a href="#new---this%E6%8C%87%E5%90%91new%E4%B9%8B%E5%90%8E%E5%BE%97%E5%88%B0%E7%9A%84%E5%AE%9E%E4%BE%8B">new - this指向new之后得到的实例</a></li>
<li><a href="#%E8%BF%BD%E9%97%AE%E7%B1%BB%E4%B8%AD%E5%BC%82%E6%AD%A5%E6%96%B9%E6%B3%95this%E6%9C%89%E5%8C%BA%E5%88%AB%E5%90%97">追问：类中异步方法，this有区别吗</a></li>
</ul>
</li>
<li><a href="#%E8%BF%BD%E9%97%AE%E5%A6%82%E4%BD%95%E7%AA%81%E7%A0%B4%E4%BD%9C%E7%94%A8%E5%9F%9F">追问：如何突破作用域？</a><ul>
<li><a href="#%E9%97%AD%E5%8C%85%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0%E5%92%8C%E5%AE%83%E5%91%A8%E5%9B%B4%E7%8A%B6%E6%80%81%E7%9A%84%E5%BC%95%E7%94%A8%E6%8D%86%E7%BB%91%E5%9C%A8%E4%B8%80%E8%B5%B7%E7%9A%84%E7%BB%84%E5%90%88"><strong>闭包：一个函数和它周围状态的引用捆绑在一起的组合</strong></a></li>
<li><a href="#%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E7%9A%84%E6%97%B6%E5%80%99">函数作为参数的时候</a></li>
<li><a href="#%E5%87%BD%E6%95%B0%E5%B5%8C%E5%A5%97">函数嵌套</a></li>
<li><a href="#%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%BC%82%E6%AD%A5%E7%9A%84%E9%97%AD%E5%8C%85">事件处理（异步）的闭包</a></li>
<li><a href="#%E8%BF%BD%E9%97%AE%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0--%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%B5%8C%E5%A5%97--%E6%8B%A5%E6%9C%89%E7%8B%AC%E7%AB%8B%E4%BD%9C%E7%94%A8%E5%9F%9F">追问:立即执行函数 &#x2F; 立即执行嵌套 &#x3D;&gt; 拥有独立作用域</a></li>
<li><a href="#%E9%97%AD%E5%8C%85%E5%AE%9E%E7%8E%B0%E7%A7%81%E6%9C%89%E5%8F%98%E9%87%8F---%E9%AB%98%E9%A2%91">闭包：实现私有变量 - 高频</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="作用域-上下文"><a href="#作用域-上下文" class="headerlink" title="作用域 &amp; 上下文"></a>作用域 &amp; 上下文</h2><h3 id="作用域链"><a href="#作用域链" class="headerlink" title="作用域链"></a>作用域链</h3><p><strong>面试题</strong>  </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">let</span> a= <span class="string">&#x27;global&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">course</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">let</span> b = <span class="string">&#x27;bbb&#x27;</span></span><br><span class="line"></span><br><span class="line">        <span class="title function_">session</span>()</span><br><span class="line">        fuction <span class="title function_">session</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="keyword">let</span> c = <span class="string">&#x27;ccc&#x27;</span></span><br><span class="line"></span><br><span class="line">            <span class="title function_">teacher</span>()</span><br><span class="line">            fuction <span class="title function_">teacher</span>(<span class="params"></span>)&#123;</span><br><span class="line">                <span class="comment">// let d = &#x27;ddd&#x27;</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">//变量提升,提升范围在当前作用域下</span></span><br><span class="line">                <span class="comment">//var存在声明提升和赋值提升，可以提前访问，结果为undefined</span></span><br><span class="line">                <span class="comment">//let仅仅存在声明提升，不会给它初始化，更不会给他赋值，暂时性死区,真正赋值才会初始化</span></span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(d)</span><br><span class="line">                <span class="keyword">var</span> d = <span class="string">&#x27;yy&#x27;</span></span><br><span class="line"></span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(d);<span class="comment">//作用域生效</span></span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(b);<span class="comment">//作用域向上查找</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">course</span>()</span><br><span class="line"></span><br><span class="line">    <span class="comment">// **************</span></span><br><span class="line">    <span class="comment">//提升优先级 =&gt; 函数需要变量</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(xxx)</span><br><span class="line">    <span class="keyword">var</span> xxx =<span class="number">123</span></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">xxx</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;functionXXX&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(xxx)</span><br><span class="line"></span><br><span class="line">    <span class="comment">//打印结果：</span></span><br><span class="line">    <span class="comment">// ƒ xxx()&#123;</span></span><br><span class="line">    <span class="comment">//     console.log(&#x27;functionXXX&#x27;)</span></span><br><span class="line">    <span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 123</span></span><br><span class="line">    <span class="comment">//等价于</span></span><br><span class="line">    <span class="comment">//1.var xxx</span></span><br><span class="line">    <span class="comment">//2.function xxx()&#123;&#125;</span></span><br><span class="line">    <span class="comment">//3.console.log(xxx)//xxx()</span></span><br><span class="line">    <span class="comment">//4.xxx=123</span></span><br><span class="line">    <span class="comment">//5.console.log(xxx)//123</span></span><br><span class="line">    <span class="comment">//变量和函数同时提升时 </span></span><br><span class="line">    <span class="comment">//提升维度：变量优先  执行维度：函数优先</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// ********************</span></span><br><span class="line">    <span class="comment">// 结论：函数是天然的作用域隔离方案 =&gt; 模块化基础</span></span><br><span class="line"><span class="string">``</span><span class="string">`  </span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">- 1. 对于作用域链 我们可以用创建态去定位链条中的某一环  </span></span><br><span class="line"><span class="string">- 2. 手动取消链条环甚至全局作用域的时候，可以利用块级作用域做性能优化 =&gt; 性能优化</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">### this 上下文 context  </span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">- this是在执行过程中动态读取的上下文所决定的</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">考察重点 - 各**使用态**中的指针指向</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">#### 函数直接调用 - this指向是window  </span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">`</span><span class="string">``</span>js</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;函数内部this&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">foo</span>()<span class="comment">//打印window</span></span><br></pre></td></tr></table></figure>

<h4 id="隐式绑定-this指向调用堆栈的上一级"><a href="#隐式绑定-this指向调用堆栈的上一级" class="headerlink" title="隐式绑定 - this指向调用堆栈的上一级"></a>隐式绑定 - this指向调用堆栈的上一级</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;隐式绑定&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">    <span class="attr">a</span>:<span class="number">1</span>,</span><br><span class="line">    fn</span><br><span class="line">&#125;</span><br><span class="line">obj.<span class="property">fn</span> = fn</span><br><span class="line">obj.<span class="title function_">fn</span>()<span class="comment">//打印这个obj,既 &#123;a:1,fn:f&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="面试题"><a href="#面试题" class="headerlink" title="面试题"></a>面试题</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> foo = &#123;</span><br><span class="line">    <span class="attr">bar</span>:<span class="number">10</span>,</span><br><span class="line">    <span class="attr">fn</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">bar</span>)</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">    <span class="comment">//取出</span></span><br><span class="line">    <span class="keyword">let</span> fn1 = foo.<span class="property">fn</span></span><br><span class="line">    <span class="title function_">fn1</span>()<span class="comment">//取出后打印为window，查看执行态</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//追问:</span></span><br><span class="line">    <span class="keyword">const</span> o1 = &#123;</span><br><span class="line">        <span class="attr">text</span>:<span class="string">&#x27;o1&#x27;</span>,</span><br><span class="line">        <span class="attr">fn</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="comment">// 直接使用上下文</span></span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;o1fn_this&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">text</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">const</span> o2 = &#123;</span><br><span class="line">        <span class="attr">text</span>:<span class="string">&#x27;o2&#x27;</span>,</span><br><span class="line">        <span class="attr">fn</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;o2fn_this&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">            <span class="comment">//调用者还是o1</span></span><br><span class="line">            <span class="keyword">return</span> o1.<span class="title function_">fn</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> o3 = &#123;</span><br><span class="line">        <span class="attr">text</span>:<span class="string">&#x27;o3&#x27;</span>,</span><br><span class="line">        <span class="attr">fn</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;o3fn_this&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">            <span class="keyword">let</span> fn = o1.<span class="property">fn</span></span><br><span class="line">            <span class="comment">// 找不到调用者，this指向windows</span></span><br><span class="line">            <span class="keyword">return</span> <span class="title function_">fn</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;o1&#x27;</span>,o1.<span class="title function_">fn</span>())</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;o2&#x27;</span>,o2.<span class="title function_">fn</span>())</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;o3&#x27;</span>,o3.<span class="title function_">fn</span>())</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>运行结果:<br><img data-src="" data-original="https://ooo.0x0.ooo/2023/11/27/OrDIMi.png" alt="OrDIMi.png"></p>
</blockquote>
<ul>
<li><ol>
<li>在执行函数的时候，函数执行时调用方上一级 &#x3D;&gt; 上下文</li>
</ol>
</li>
<li><ol start="2">
<li>公共函数 | 全局调用 &#x3D;&gt; window</li>
</ol>
</li>
</ul>
<h4 id="显示绑定-bind-apply-call"><a href="#显示绑定-bind-apply-call" class="headerlink" title="显示绑定(bind | apply | call)"></a>显示绑定(bind | apply | call)</h4><ul>
<li><ol>
<li>call &lt; &#x3D; &gt; apply 传参不同 依次传入 &#x2F; 数组传入</li>
</ol>
</li>
<li><ol start="2">
<li>bind 返回值不同</li>
</ol>
</li>
</ul>
<p><strong>面试</strong>：手写apply &amp; bind</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.需求:手写bind =&gt; bind位置 =&gt; Function.prototype =&gt; 原型</span></span><br><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">newBind</span> = <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="comment">//1.1 bind原理 改变this</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 暂存this，因为newBind要指向原函数</span></span><br><span class="line">    <span class="keyword">const</span> _this = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="comment">// Array.prototype.slice.call(arguments)能够将具有length属性的arguments转换为数组,  我们可以理解为就是 arguments.toArray().slice()</span></span><br><span class="line">    <span class="keyword">const</span> args = <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">slice</span>.<span class="title function_">call</span>(<span class="variable language_">arguments</span>)</span><br><span class="line">    <span class="comment">// 获取栈顶，既是shift</span></span><br><span class="line">    <span class="keyword">const</span> newThis = args.<span class="title function_">shift</span>()</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.2返回值不执行 =&gt; 返回函数</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="comment">// 新函数传的参数，与bind传入的参数进行拼接</span></span><br><span class="line">        <span class="keyword">const</span> restArgs = <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">slice</span>.<span class="title function_">call</span>(<span class="variable language_">arguments</span>)</span><br><span class="line">        <span class="keyword">const</span> allArgs = args.<span class="title function_">concat</span>(restArgs)</span><br><span class="line">        <span class="comment">// 执行核心，原函数执行newApply，但是上下文指向newThis</span></span><br><span class="line">        <span class="comment">//return _this.apply(newThis,allArgs)</span></span><br><span class="line">        <span class="keyword">return</span> _this.<span class="title function_">newApply</span>(newThis,allArgs)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//  2.内层实现</span></span><br><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">newApply</span> = <span class="keyword">function</span>(<span class="params">context</span>)&#123;</span><br><span class="line">    <span class="comment">// 函数检测</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="variable language_">this</span> !== <span class="string">&#x27;function&#x27;</span>)&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&#x27;type error&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 参数兜底</span></span><br><span class="line">    context = context || <span class="variable language_">window</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 临时挂载执行函数</span></span><br><span class="line">    <span class="comment">// 根据谁调用指向谁的原则，将this指向赋值给context（newThis）的一个属性上</span></span><br><span class="line">    <span class="comment">// 相当于调用了newThis.fn() 因此上下文为newThis</span></span><br><span class="line">    context.<span class="property">fn</span> = <span class="variable language_">this</span></span><br><span class="line">    <span class="keyword">let</span> result = <span class="variable language_">arguments</span>[<span class="number">1</span>]<span class="comment">//有值的话说明有参数</span></span><br><span class="line">        ? context.<span class="title function_">fn</span>(...<span class="variable language_">arguments</span>[<span class="number">1</span>])</span><br><span class="line">        : context.<span class="title function_">fn</span>()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> context.<span class="property">fn</span></span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="new-this指向new之后得到的实例"><a href="#new-this指向new之后得到的实例" class="headerlink" title="new - this指向new之后得到的实例"></a>new - this指向new之后得到的实例</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Course</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">name</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;构造函数中的this&#x27;</span>,<span class="variable language_">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">test</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;类方法中的this&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> course = <span class="keyword">new</span> <span class="title class_">Course</span>(<span class="string">&#x27;this&#x27;</span>)</span><br><span class="line">course.<span class="title function_">test</span>()</span><br></pre></td></tr></table></figure>

<h4 id="追问：类中异步方法，this有区别吗"><a href="#追问：类中异步方法，this有区别吗" class="headerlink" title="追问：类中异步方法，this有区别吗"></a>追问：类中异步方法，this有区别吗</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Course</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">name</span>)&#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;构造函数中的this:&#x27;</span>,<span class="variable language_">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">test</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;类方法中的this:&#x27;</span>,<span class="variable language_">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="title function_">asyncTest</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;异步方法外:&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;异步方法内:&#x27;</span>,<span class="variable language_">this</span>)</span><br><span class="line">        &#125;,<span class="number">500</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> course = <span class="keyword">new</span> <span class="title class_">Course</span>(<span class="string">&#x27;this&#x27;</span>);</span><br><span class="line">course.<span class="title function_">test</span>();</span><br><span class="line">course.<span class="title function_">asyncTest</span>();</span><br></pre></td></tr></table></figure>

<blockquote>
<p>运行结果<br><img data-src="" data-original="https://ooo.0x0.ooo/2023/11/28/OrGWMC.png" alt="OrGWMC.png"></p>
</blockquote>
<ul>
<li><ol>
<li>执行setTimeout时，匿名方法执行上下文，在队列和全局执行函数效果相同 - 指向window</li>
</ol>
</li>
<li><ol start="2">
<li>再追问，如何解决 - 记录this &#x2F; 显示 &#x2F; 箭头函数</li>
</ol>
</li>
</ul>
<h3 id="追问：如何突破作用域？"><a href="#追问：如何突破作用域？" class="headerlink" title="追问：如何突破作用域？"></a>追问：如何突破作用域？</h3><h4 id="闭包：一个函数和它周围状态的引用捆绑在一起的组合"><a href="#闭包：一个函数和它周围状态的引用捆绑在一起的组合" class="headerlink" title="闭包：一个函数和它周围状态的引用捆绑在一起的组合"></a><strong>闭包：一个函数和它周围状态的引用捆绑在一起的组合</strong></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">mail</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">let</span> content = <span class="string">&#x27;信&#x27;</span></span><br><span class="line">    <span class="comment">//包裹起来传递到另一个作用域，突破原作用域</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> content</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> envelop = <span class="title function_">mail</span>()</span><br><span class="line"><span class="title function_">envelop</span>()</span><br></pre></td></tr></table></figure>

<ul>
<li><p>函数可以作为返回值传递的</p>
</li>
<li><p>函数外部可以通过一定方式获取到内部的作用域变量 &#x3D;&gt; 导致内部变量不被GC</p>
</li>
</ul>
<h4 id="函数作为参数的时候"><a href="#函数作为参数的时候" class="headerlink" title="函数作为参数的时候"></a>函数作为参数的时候</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 单一职责</span></span><br><span class="line"><span class="keyword">let</span> content;</span><br><span class="line"><span class="comment">// 通用存储</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">envelop</span>(<span class="params">fn</span>)&#123;</span><br><span class="line">    content = <span class="number">1</span>;</span><br><span class="line">    <span class="title function_">fn</span>();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 业务逻辑</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">mail</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(content);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">envelop</span>(mail);</span><br></pre></td></tr></table></figure>

<h4 id="函数嵌套"><a href="#函数嵌套" class="headerlink" title="函数嵌套"></a>函数嵌套</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> counter = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">outerFn</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">innerFn</span>(<span class="params"></span>)&#123;</span><br><span class="line">        counter++;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(counter);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> innerFn;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">outerFn</span>()();</span><br></pre></td></tr></table></figure>

<h4 id="事件处理（异步）的闭包"><a href="#事件处理（异步）的闭包" class="headerlink" title="事件处理（异步）的闭包"></a>事件处理（异步）的闭包</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> lis = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;li&#x27;</span>);</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i&lt;lis.<span class="property">length</span>;i++)&#123;</span><br><span class="line">    (<span class="keyword">function</span>(<span class="params">i</span>)&#123;</span><br><span class="line">        lis[i].<span class="property">onclick</span> = <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(i)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)(i);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="追问-立即执行函数-立即执行嵌套-拥有独立作用域"><a href="#追问-立即执行函数-立即执行嵌套-拥有独立作用域" class="headerlink" title="追问:立即执行函数 &#x2F; 立即执行嵌套 &#x3D;&gt; 拥有独立作用域"></a>追问:立即执行函数 &#x2F; 立即执行嵌套 &#x3D;&gt; 拥有独立作用域</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">(<span class="keyword">function</span> <span class="title function_">immediateA</span>(<span class="params">a</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="keyword">function</span> <span class="title function_">immediateB</span>(<span class="params">b</span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(a);</span><br><span class="line">    &#125;)(<span class="number">1</span>);</span><br><span class="line">&#125;)(<span class="number">0</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>推动了js的模块化发展</li>
</ul>
<h4 id="闭包：实现私有变量-高频"><a href="#闭包：实现私有变量-高频" class="headerlink" title="闭包：实现私有变量 - 高频"></a>闭包：实现私有变量 - 高频</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> stack = &#123;</span><br><span class="line">    <span class="attr">items</span>:[],</span><br><span class="line">    <span class="attr">push</span>:<span class="keyword">function</span>(<span class="params"></span>)&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">createStack</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">const</span> items = [];</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="title function_">push</span>(<span class="params">item</span>)&#123;</span><br><span class="line">            items.<span class="title function_">push</span>(item)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">setItems</span>(<span class="params"></span>)&#123;&#125;,</span><br><span class="line">        <span class="title function_">getItems</span>(<span class="params"></span>)&#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

    </div>

    
    
    
    
      <div>
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:16px;">-------------本文结束<i class="fa fa-bolt"></i>感谢阅读-------------</div>
    
</div>
      </div>
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>右眼
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://jjbond.cn/posts/4503.html" title="this指针&#x2F;闭包&#x2F;作用域">https://jjbond.cn/posts/4503.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Web/" rel="tag"><i class="fa fa-tag"></i> Web</a>
              <a href="/tags/JavaScript/" rel="tag"><i class="fa fa-tag"></i> JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/36796.html" rel="prev" title="什么是Nginx?">
      <i class="fa fa-chevron-left"></i> 什么是Nginx?
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/3835.html" rel="next" title="OOP与继承">
      OOP与继承 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC81ODY4My8zNTE0NQ=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#2023-11-26-this%E6%8C%87%E9%92%88-%E9%97%AD%E5%8C%85-%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">1.</span> <span class="nav-text">2023&#x2F;11&#x2F;26 this指针&#x2F;闭包&#x2F;作用域</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F-%E4%B8%8A%E4%B8%8B%E6%96%87"><span class="nav-number">1.1.</span> <span class="nav-text">作用域 &amp; 上下文</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F%E9%93%BE"><span class="nav-number">1.1.1.</span> <span class="nav-text">作用域链</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9A%90%E5%BC%8F%E7%BB%91%E5%AE%9A-this%E6%8C%87%E5%90%91%E8%B0%83%E7%94%A8%E5%A0%86%E6%A0%88%E7%9A%84%E4%B8%8A%E4%B8%80%E7%BA%A7"><span class="nav-number">1.1.1.1.</span> <span class="nav-text">隐式绑定 - this指向调用堆栈的上一级</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="nav-number">1.1.1.2.</span> <span class="nav-text">面试题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%98%BE%E7%A4%BA%E7%BB%91%E5%AE%9A-bind-apply-call"><span class="nav-number">1.1.1.3.</span> <span class="nav-text">显示绑定(bind | apply | call)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#new-this%E6%8C%87%E5%90%91new%E4%B9%8B%E5%90%8E%E5%BE%97%E5%88%B0%E7%9A%84%E5%AE%9E%E4%BE%8B"><span class="nav-number">1.1.1.4.</span> <span class="nav-text">new - this指向new之后得到的实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%BD%E9%97%AE%EF%BC%9A%E7%B1%BB%E4%B8%AD%E5%BC%82%E6%AD%A5%E6%96%B9%E6%B3%95%EF%BC%8Cthis%E6%9C%89%E5%8C%BA%E5%88%AB%E5%90%97"><span class="nav-number">1.1.1.5.</span> <span class="nav-text">追问：类中异步方法，this有区别吗</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%BD%E9%97%AE%EF%BC%9A%E5%A6%82%E4%BD%95%E7%AA%81%E7%A0%B4%E4%BD%9C%E7%94%A8%E5%9F%9F%EF%BC%9F"><span class="nav-number">1.1.2.</span> <span class="nav-text">追问：如何突破作用域？</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%97%AD%E5%8C%85%EF%BC%9A%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0%E5%92%8C%E5%AE%83%E5%91%A8%E5%9B%B4%E7%8A%B6%E6%80%81%E7%9A%84%E5%BC%95%E7%94%A8%E6%8D%86%E7%BB%91%E5%9C%A8%E4%B8%80%E8%B5%B7%E7%9A%84%E7%BB%84%E5%90%88"><span class="nav-number">1.1.2.1.</span> <span class="nav-text">闭包：一个函数和它周围状态的引用捆绑在一起的组合</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E7%9A%84%E6%97%B6%E5%80%99"><span class="nav-number">1.1.2.2.</span> <span class="nav-text">函数作为参数的时候</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%B5%8C%E5%A5%97"><span class="nav-number">1.1.2.3.</span> <span class="nav-text">函数嵌套</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%EF%BC%88%E5%BC%82%E6%AD%A5%EF%BC%89%E7%9A%84%E9%97%AD%E5%8C%85"><span class="nav-number">1.1.2.4.</span> <span class="nav-text">事件处理（异步）的闭包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%BD%E9%97%AE-%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0-%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%B5%8C%E5%A5%97-%E6%8B%A5%E6%9C%89%E7%8B%AC%E7%AB%8B%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">1.1.2.5.</span> <span class="nav-text">追问:立即执行函数 &#x2F; 立即执行嵌套 &#x3D;&gt; 拥有独立作用域</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%97%AD%E5%8C%85%EF%BC%9A%E5%AE%9E%E7%8E%B0%E7%A7%81%E6%9C%89%E5%8F%98%E9%87%8F-%E9%AB%98%E9%A2%91"><span class="nav-number">1.1.2.6.</span> <span class="nav-text">闭包：实现私有变量 - 高频</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="右眼"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">右眼</p>
  <div class="site-description" itemprop="description">任何时间 任何地点<br>超级侦探 认真办案</div>
</div>


<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">18</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Neopo1itan/" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Neopo1itan&#x2F;" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/youyan030" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;youyan030" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://steamcommunity.com/id/youyanww/" title="Steam → https:&#x2F;&#x2F;steamcommunity.com&#x2F;id&#x2F;youyanww&#x2F;" rel="noopener" target="_blank"><i class="fab fa-steam fa-fw"></i>Steam</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://space.bilibili.com/895680/video" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;895680&#x2F;video" rel="noopener" target="_blank"><i class="fa fa-wheelchair fa-fw"></i>BiliBili</a>
      </span>
  </div>



      </div>
      
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=320 height=240 src="//music.163.com/outchain/player?type=0&id=8545160772&auto=1&height=430"></iframe>
    
      <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
      <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
        <div class="widget-wrap">
          <div id="myCanvasContainer" class="widget tagcloud">
            <canvas width="250" height="250" id="resCanvas" style="width:100%">
              <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/JS/" rel="tag">JS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/" rel="tag">JavaScript</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React/" rel="tag">React</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue/" rel="tag">Vue</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Web/" rel="tag">Web</a><span class="tag-list-count">15</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/" rel="tag">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a><span class="tag-list-count">1</span></li></ul>
            </canvas>
          </div>
        </div>
    
    </div>

  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">右眼</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">59k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">53 分钟</span>
</div>
  <div class="powered-by">
    <!--由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动 -->
    <span>个人网站&nbsp&nbsp&nbsp|&nbsp&nbsp&nbsp吉吉邦德</span>
  </div>



<!--添加运行时间--> 
</br>
<span id="sitetime"></span> 
<script language=javascript> 
  function siteTime(){ 
    window.setTimeout("siteTime()", 1000); 
    var seconds = 1000; 
    var minutes = seconds * 60; 
    var hours = minutes * 60; 
    var days = hours * 24; 
    var years = days * 365; 
    var today = new Date(); 
    var todayYear = today.getFullYear(); 
    var todayMonth = today.getMonth()+1; 
    var todayDate = today.getDate(); 
    var todayHour = today.getHours(); 
    var todayMinute = today.getMinutes(); 
    var todaySecond = today.getSeconds(); 
    /* 
    Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳) 
    year - 作为date对象的年份，为4位年份值 
    month - 0-11之间的整数，做为date对象的月份 
    day - 1-31之间的整数，做为date对象的天数 
    hours - 0(午夜24点)-23之间的整数，做为date对象的小时数 
    minutes - 0-59之间的整数，做为date对象的分钟数 
    seconds - 0-59之间的整数，做为date对象的秒数 
    microseconds - 0-999之间的整数，做为date对象的毫秒数 
    */ 
    var t1 = Date.UTC(2023,07,06,15,49,36); //北京时间2018-2-13 00:00:00 
    var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond); 
    var diff = t2-t1; 
    var diffYears = Math.floor(diff/years); 
    var diffDays = Math.floor((diff/days)-diffYears*365); 
    var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours); 
    var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes); 
    var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds); 
    document.getElementById("sitetime").innerHTML=" 已运行"+/*diffYears+" 年 "+*/diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒"; 
    } 
    siteTime(); 
</script> 
<!--// 添加运行时间-->

        








      </div>
    </footer>
  </div>

  
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
    

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

    </div>

        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: true,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script><script async>window.onload=function(){var a=document.createElement('script'),b=document.getElementsByTagName('script')[0];a.type='text/javascript',a.async=!0,a.src='/sw-register.js?v='+Date.now(),b.parentNode.insertBefore(a,b)};</script></body></html>